var QRCode = require('../../utils/weapp-qrcode.js');
var qrcode;
const app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: app.globalData.id || wx.getStorageSync("id"),
    orderNo: '',
    restaurantName: '',
    status: '',
    type: '', //1店内就餐 2预定取餐
    pickUpTime: '',
    dataList: [],
    showModal: false,
    userId: '',
    totalAmount: '',
    restaurantId: '',
    orderNo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      orderNo: options.orderNo,
    })
    that.getData();
    that.getqrcode();
  },
  gorestaurantevaluation(e) { //评价
    var orderNo = e.currentTarget.dataset.orderno;
    var restaurantId = e.currentTarget.dataset.restaurantid;
    wx.navigateTo({
      url: "../../pages/restaurantevaluation/restaurantevaluation?orderNo=" + orderNo + '&restaurantId=' + restaurantId
    })
  },
  //获取订单详情
  getData() {
    var that = this;
    var orderNo = that.data.orderNo;

    var userId = app.globalData.id || wx.getStorageSync("id");
    that.setData({
      userId: userId
    })
    var url = '/api/restaurantOrders/' + that.data.orderNo;
    if (userId) {
      wx.request({
        url: applet_park_api_url + '/api/restaurantOrders/' + that.data.orderNo,
        method: 'get',
        data: {},
        success: function (res) {

          if (res.data.code == 0) {
            // for (var j = 0; j < res.data.data.foods.length; j++) {
            //   res.data.data.foods[j].dishImage = that.getPic(res.data.data.foods[j].dishImage);
            // }
            var foods = [];
            var item;
            for (item of res.data.data.foods) {
              item.dishImage = that.getPic(item.dishImage);
              foods.push(item)
            }
            that.setData({
              dataList: foods,
              restaurantName: res.data.data.restaurantName,
              status: res.data.data.status,
              pickUpTime: res.data.data.pickUpTime,
              type: res.data.data.type,
              totalAmount: res.data.data.totalAmount,
              restaurantId: res.data.data.restaurantId,
              orderNo: res.data.data.orderNo,
            });

          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            });
          }
          wx.hideLoading();

        },
        fail: function (res) {
          wx.showToast({
            title: '网络异常，请稍后重试',
          })
          wx.hideLoading();

        }

      })


      requestDataparams(url, {}, 'GET').then(res => {
        if (res.data.code == 0) {
          // for (var j = 0; j < res.data.data.foods.length; j++) {
          //   res.data.data.foods[j].dishImage = that.getPic(res.data.data.foods[j].dishImage);
          // }
          var foods = [];
          var item;
          for (item of res.data.data.foods) {
            item.dishImage = that.getPic(item.dishImage);
            foods.push(item)
          }
          that.setData({
            dataList: foods,
            restaurantName: res.data.data.restaurantName,
            status: res.data.data.status,
            pickUpTime: res.data.data.pickUpTime,
            type: res.data.data.type,
          });

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
        wx.hideLoading();
      }).catch(err => {

        wx.showToast({
          title: '网络异常，请稍后重试',
        })
        wx.hideLoading();
      });
    }
  },
  //处理门店头像和食品头像
  getPic(e) {
    var imgUrl = e.split(',')[0];
    imgUrl = applet_park_api_url_imgUrl + '/' + imgUrl;
    //
    return imgUrl;
  },
  getqrcode() {
    var that = this;
    qrcode = new QRCode('canvas', {
      text: that.data.orderNo,
      width: 80,
      height: 80,
      colorDark: "#000000",
      colorLight: "#fff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  enlargeQrcode() {
    var that = this;
    that.setData({
      showModal: true
    })
    qrcode = new QRCode('canvas1', {
      text: that.data.orderNo,
      width: 250,
      height: 250,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  hideModal() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      showModal: false
    });
    that.getData();
    wx.hideLoading();
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.getData();
  },
  pay() {
    let that = this;
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var id = app.globalData.id || wx.getStorageSync("id");
    var orderNo = that.data.orderno;
    var totalAmount = parseInt(that.data.totalAmount) * 100;
    var remark = that.data.remark;
    var couponId = that.data.couponId;
    var TOKEN = app.globalData.TOKEN || wx.getStorageSync("TOKEN");
    wx.request({
      url: applet_park_api_url + '/api/wechatPay',
      method: 'POST',
      header: {
        Authorization: TOKEN
      },
      data: {
        orderNo: that.data.orderNo,
        totalAmount: totalAmount,
        sysUserId: id,
        openid: openId,
        remark: remark,
        couponId: couponId,
        paySource: 1
      },
      success(res2) {

        if (res2.data.code == 0) {




          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })

              setTimeout(function () {
                wx.showToast({
                  title: '页面跳转中',
                })
                that.getData()
              }, 1000);
            },
            fail(res3) {

            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})